package com.ffapp.rainmonitorcustom.user.message;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.ffapp.rainmonitorcustom.alarm.AlarmDetailActivity;
import com.ffapp.rainmonitorcustom.R;
import com.ffapp.rainmonitorcustom.basis.BasisFragment;
import com.ffapp.rainmonitorcustom.basis.Constants;
import com.ffapp.rainmonitorcustom.bean.AlarmListData;
import com.ffapp.rainmonitorcustom.bean.BaseListBeanYL;
import com.ffapp.rainmonitorcustom.bean.BasisBean;
import com.ffapp.rainmonitorcustom.bean.UserMsgListBean;
import com.ffapp.rainmonitorcustom.bean.UserMsgListData;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.loopj.android.http.RequestParams;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import my.ActivityTool;
import my.http.HttpRestClient;
import my.http.MyHttpListener;

/**
 * 我的消息 火警告警
 * */
public class MsgFiralarmListFragment extends BasisFragment {

    public static MsgFiralarmListFragment newInstance(int type ) {

        MsgFiralarmListFragment fragment = new MsgFiralarmListFragment();
        fragment.type = type;
        return fragment;
    }

    public static final int TYPE_ALL = 0,TYPE_NORMAL = 1 ,TYPE_FIRALARM = 2;


    @BindView(R.id.recyclerview)
    XRecyclerView mRecyclerView;

    private MyAdapterList mAdapter;
    private UserMsgListBean mListBean;

    int  type;

    @Override
    public void initViews() {
        super.initViews();
        setContentView(R.layout.base_xrecycler_with_empty);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setVerticalScrollBarEnabled(true);
        mRecyclerView.setLayoutManager(layoutManager);
//        mRecyclerView.setEmptyView(findViewById(R.id.refresh_view));
        mRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        mRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
        mRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mListBean.refresh();
                getList();
            }

            @Override
            public void onLoadMore() {
                getList();
            }
        });
        mRecyclerView.setPullRefreshEnabled(true);
        mRecyclerView.setLoadingMoreEnabled(true);

    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);

        mListBean = new UserMsgListBean();
//        BaseListData data;
//        for(int i = 0,l = 30; i < l ;i ++){
//            data = new BaseListData("日常巡查","日常巡查",R.drawable.manage_icon_daily,R.drawable.manage_bg_item_1);
//            mListBean.getList().add(data);
//        }

        setListView();
//        mRecyclerView.refresh();
        getList();
//        getFromCache();
    }

//    private void getFromCache() {
//
//        DothingsListBean beanCaceh = (DothingsListBean) DothingsListBean.getObjFromCache(id);
//        if (beanCaceh != null) {
//            mListBean = beanCaceh;
//            setListView();
//        } else {
//            mListBean = new ServiceNewListBean();
//            setListView();
//            getList();
//        }
//    }

    private void setListView() {
        if (mAdapter == null) {
            mAdapter = new MyAdapterList();
            mRecyclerView.setAdapter(mAdapter);
        } else {
            mAdapter.notifyDataSetChanged();
        }
    }

    private void onListViewComplete() {
        mRecyclerView.refreshComplete();
        mRecyclerView.loadMoreComplete();
        mRecyclerView.setLoadingMoreEnabled(mListBean.hasNextPage());
//        mRecyclerView.setLoadingMoreEnabled(false);
        setEmptyView();
    }

    private void setEmptyView() {
        if (mListBean == null || mListBean.getList().size() == 0) {
            findViewById(R.id.refresh_view).setVisibility(View.VISIBLE);
            findViewById(R.id.refresh_img).setVisibility(View.VISIBLE);
            ((ImageView)findViewById(R.id.refresh_img)).setImageResource(R.drawable.user_msg_img_empty);
            findViewById(R.id.refresh_text_empty).setVisibility(View.VISIBLE);
            ((TextView)findViewById(R.id.refresh_text_empty)).setText("暂无任何消息");
            findViewById(R.id.refresh_view).setOnClickListener(
                    new View.OnClickListener() {
                        public void onClick(View v) {
                            mRecyclerView.refresh();
                        }
                    });
        } else {
            findViewById(R.id.refresh_view).setVisibility(View.INVISIBLE);
        }
    }

    private void getList() {
        RequestParams params = new RequestParams();
        showProgress();
        params.put("msgType", type);
        params.put("beginTime", "");
        params.put("endTime", "");

        params.put(BaseListBeanYL.PAGE_NAME, mListBean.getNextPage());
        params.put(BaseListBeanYL.PAGE_SIZE_NAME, BaseListBeanYL.PAGE_SIZE);
        HttpRestClient.post(Constants.URL_USER_MSG_LIST, params, myHttpListener,
                HTTP_LIST, UserMsgListBean.class);

    }

    private final static int HTTP_LIST = 11;
    MyHttpListener myHttpListener = new MyHttpListener() {
        @Override
        public void onSuccess(int httpWhat, Object result) {
            UserMsgListBean bean = (UserMsgListBean) result;
            mListBean.addListBean(bean);
            setListView();
        }

        @Override
        public void onFailure(int httpWhat, Object result) {
            super.onFailure(httpWhat, result);
//            mListBean = new ServiceNewListBean();
            setListView();
        }

        @Override
        public void onFinish(int httpWhat) {
            hideLoading();
            onListViewComplete();
        }
    };

    public class MyAdapterList extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

        public static final int VIEW_TYPE_NORMAL = 1,VIEW_TYPE_FIRALARM = 2;

        public MyAdapterList() {

        }

        //创建新View，被LayoutManager所调用
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
            RecyclerView.ViewHolder viewHolder = null;
            switch (viewType){
                case  VIEW_TYPE_NORMAL:
                    viewHolder = new ViewHolder( LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.user_msg_item_normal, viewGroup, false));
                    break;
                case  VIEW_TYPE_FIRALARM:
                    viewHolder = new ViewHolderFiralarm( LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.user_msg_item_firalarm, viewGroup, false));
                    break;
            }
            return viewHolder;
        }


        //将数据与界面进行绑定的操作
        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
            UserMsgListData data = mListBean.getList().get(position);
            switch (viewHolder.getItemViewType()){
                case VIEW_TYPE_NORMAL:
                    ((ViewHolder)viewHolder).bind(data);
                    break;
                case VIEW_TYPE_FIRALARM:
                    ((ViewHolderFiralarm)viewHolder).bind(position);
                    break;
            }

        }

        //获取数据的数量
        @Override
        public int getItemCount() {
            return mListBean == null ? 0 : mListBean.getList().size();
        }

        @Override
        public int getItemViewType(int position) {
            return Integer.parseInt(mListBean.getList().get(position).getMSGTYPE());
//            return super.getItemViewType(position);
        }

        //自定义的ViewHolder，持有每个Item的的所有界面元素
        public class ViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.list_item)
            public View list_item;
            @BindView(R.id.text_title)
            public TextView text_title;
            @BindView(R.id.text_time)
            public TextView text_time;

            @BindView(R.id.text_content)
            public TextView text_content;
            @BindView(R.id.text_read)
            public TextView text_read;
            @BindView(R.id.view_detail)
            public View view_detail;


            public ViewHolder(View view) {
                super(view);
                ButterKnife.bind(this, view);

                text_read.setVisibility(View.GONE);
            }
            
            public void bind(UserMsgListData data){
                text_title.setText(data.getTITLE());
//                text_content.setText(data.getCONTENT());
                text_content.setText(data.getCONTENTStr());
                text_time.setText(data.getCREATETIME());
                list_item.setTag(data);
            }

//            @OnClick(R.id.list_item)
//            public void onItemClick(View v) {
//                    MonitorListData data = (MonitorListData) v.getTag();
//                Bundle extras = new Bundle();
//                extras.putSerializable("data",data);
//                ActivityTool.skipActivity(mContext, MonitorDetailActivity.class,extras);
//            }
        }

        //自定义的ViewHolder，持有每个Item的的所有界面元素
        public class ViewHolderFiralarm extends RecyclerView.ViewHolder {
            @BindView(R.id.list_item)
            public View list_item;
            @BindView(R.id.text_title)
            public TextView text_title;

            @BindView(R.id.text_content)
            public TextView text_content;
            @BindView(R.id.text_read)
            public TextView text_read;
            @BindView(R.id.view_detail)
            public View view_detail;


            public ViewHolderFiralarm(View view) {
                super(view);
                ButterKnife.bind(this, view);
//                text_read.setVisibility(View.GONE);

            }

            public void bind(int position ){
                UserMsgListData data = mListBean.getList().get(position);
                text_title.setText(data.getTITLE());
                text_content.setText(data.getCONTENTStr());
                text_read.setText(data.getREADSTATUS_Boolean()?"已读":"未读");
                list_item.setSelected(data.getREADSTATUS_Boolean());
                list_item.setTag(position);
            }

            @OnClick(R.id.list_item)
            public void onItemClick(View v) {
                int position = (int) v.getTag();
                UserMsgListData data = mListBean.getList().get(position);
                AlarmListData dataAlarm = data.getAlarmListData();
                data.setREADSTATUS("2");
//                notifyItemChanged(position);
                setRead(data.getID());
                notifyDataSetChanged();
//                NewsTextDetailActivity.toDetail(mContext,data.getArticle_id());
                Bundle extras = new Bundle();
                extras.putSerializable("data",dataAlarm);
                ActivityTool.skipActivityForResult(mContext, AlarmDetailActivity.class,extras,1);
            }
        }
    }

    private void setRead(String id) {
        RequestParams params = new RequestParams();
        params.put("id", id);
        HttpRestClient.post(Constants.URL_USER_MSG_SETREAD, params, new MyHttpListener() {
                    @Override
                    public void onSuccess(int httpWhat, Object result) {

                    }

                    @Override
                    public void onFailure(int httpWhat, Object result) {
//                        super.onFailure(httpWhat, result);
                    }

                    @Override
                    public void onFinish(int httpWhat) {

                    }
                },
                0, BasisBean.class);

    }

}
